package com.lenovo.dil.tiltbaseddemo.sensor;

import java.util.Observable;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

import com.lenovo.dil.tiltbaseddemo.MainActivity;

/**
 * @author xieqd 传感器后台进程
 */
@SuppressWarnings("deprecation")
public class SensorsProcess extends Observable {

	private static final String TAG="quickscroll";//SensorsProcess.class.getSimpleName();
	private SensorManager mSensorManager;
	public float[] mOrientationOfRotationVector = new float[3];
	public float[] mOrientation = new float[3];

	public static float[] mR = new float[16];
	public static float[] mI = new float[16];
	public static float[] mOrientationCal = new float[3];

	public static float SENSOR_ACCELEROMETER[] = null;
	float SENSOR_MAGNETIC_FIELD[] = null;
	// FlatUtil mAccelerometerFlat = new FlatUtil(0.5f);
	// FlatUtil mMagneticFieldFlat = new FlatUtil(2.5f);
	// FlatUtil mOrientationFlat = new FlatUtil(0.5f);
	private static SensorsProcess instance = new SensorsProcess();

	private SensorsProcess() {
	}

	/**
	 * @return 传感器后台进程，以便获取传感器信息
	 */
	public static SensorsProcess getInstance() {
		return instance;
	}

	/**
	 * 启动传感器进程
	 */
	public void start() {
		registerSensor();
	}

	/**
	 * 停止传感器进程
	 */
	public void stop() {
		unRegisterSensor();
	}

	public void registerSensor() {
		Log.d(TAG, "registerSensor");
		registerState=true;
		mSensorManager = (SensorManager) MainActivity.mActivity
				.getSystemService(Context.SENSOR_SERVICE);
//		mSensorManager.registerListener(mSensorEventListener,
//				mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
//				SensorManager.SENSOR_DELAY_GAME);
//		mSensorManager.registerListener(mSensorEventListener,
//				mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
//				SensorManager.SENSOR_DELAY_GAME);
//		mSensorManager.registerListener(mSensorEventListener,
//				mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
//				SensorManager.SENSOR_DELAY_GAME);
//
		mSensorManager.registerListener(mSensorEventListener,
				mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_GAME);
		mSensorManager.registerListener(mSensorEventListener,
				mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
				SensorManager.SENSOR_DELAY_GAME);
	}

	public static boolean registerState=false;
	public void unRegisterSensor() {
		Log.d(TAG, "unRegisterSensor");
		if(mSensorManager!=null){
			mSensorManager.unregisterListener(mSensorEventListener);
		}
		registerState=false;
	}
	float[] mLinearAcc;
	/**
	 * @author xieqd 传感器结果的回调处理
	 */
	SensorEventListener mSensorEventListener = new SensorEventListener() {
		@SuppressLint("NewApi")
		@Override
		public void onSensorChanged(SensorEvent event) {
//			System.out.println(event.sensor.getType()+":"+event.accuracy);
//			if(event.accuracy<=1){
//				return;
//			}
			if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
				// calibrateCheck(event.accuracy);
//				mOrientation = event.values.clone();
//				System.out.println(mOrientation[0]+":"+mOrientation[1]+":"+mOrientation[2]);
				float[] tmp = event.values.clone();
				setChanged();
				notifyObservers(tmp);
			}
			if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
				// calibrateCheck(event.accuracy);
				float[] tmp = event.values.clone();
				if(mLinearAcc!=null){
//					if(Math.abs(tmp[1]-mLinearAcc[1])>5){
//						System.out.println("old:"+mLinearAcc[0]+":"+mLinearAcc[1]+":"+mLinearAcc[2]);
//						System.out.println("new:"+tmp[0]+":"+tmp[1]+":"+tmp[2]);
//						setChanged();
//						notifyObservers(tmp[1]-mLinearAcc[1]);
//					}
//					setChanged();
//					notifyObservers(tmp[0]+":"+tmp[1]+":"+tmp[2]);
				}
				mLinearAcc = event.values.clone();
//				setChanged();
//				notifyObservers(Sensor.TYPE_ORIENTATION);
			}
			// if (event.accuracy <= SensorManager.SENSOR_STATUS_UNRELIABLE) {
			// return;
			// }
			if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
				float rotation_vector[] = event.values.clone();
				if (rotation_vector != null) {
					float[] Rin = new float[9];
					float[] Rout = new float[9];
					float[] orientation_valus = new float[3];
					SensorManager.getRotationMatrixFromVector(Rin,
							rotation_vector);
					SensorManager.remapCoordinateSystem(Rin,
							SensorManager.AXIS_X, SensorManager.AXIS_Y,
							Rout);
					SensorManager.getOrientation(Rout, orientation_valus);
					mOrientationOfRotationVector[0] = (float) Math
							.toDegrees(orientation_valus[0]);
					mOrientationOfRotationVector[1] = (float) Math
							.toDegrees(orientation_valus[1]);
					mOrientationOfRotationVector[2] = (float) Math
							.toDegrees(orientation_valus[2]);
					System.out.println(mOrientationOfRotationVector[0]+":"+mOrientationOfRotationVector[1]+":"+mOrientationOfRotationVector[2]);
					mOrientationCal = mOrientationOfRotationVector;//mOrientationFlat.flat(orientation);
					setChanged();
					notifyObservers(Sensor.TYPE_ROTATION_VECTOR);
				}
			}

			if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
				SENSOR_ACCELEROMETER = event.values.clone();
//				setChanged();
//				notifyObservers(SENSOR_ACCELEROMETER[0]+":"+SENSOR_ACCELEROMETER[1]+":"+SENSOR_ACCELEROMETER[2]);
//				notifyObservers(Sensor.TYPE_ACCELEROMETER);
			}
			if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
				SENSOR_MAGNETIC_FIELD = event.values.clone();
			}
			if (SENSOR_ACCELEROMETER != null && SENSOR_MAGNETIC_FIELD != null) {
//				float[] tmp1 = { 8.6f, -.5f, 4.5f };
//				float[] tmp2 = { 16.38f, 39.12f, -33.12f };
				SensorManager.getRotationMatrix(mR, mI, SENSOR_ACCELEROMETER,
						SENSOR_MAGNETIC_FIELD);
//				SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_Y,
//						SensorManager.AXIS_MINUS_X, mR);
				float[] orientation = new float[3];
				SensorManager.getOrientation(mR, orientation);
				orientation[0]=(float)Math.toDegrees(orientation[0]);
				orientation[1]=(float)Math.toDegrees(orientation[1]);
				orientation[2]=(float)Math.toDegrees(orientation[2]);
				
//				System.out.println(Math.toDegrees(orientation[0])+":"+Math.toDegrees(orientation[1])+":"+Math.toDegrees(orientation[2]));
//				mOrientationCal = mOrientationFlat.flat(orientation);
				setChanged();
				notifyObservers(orientation);
			}
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}
		
	};

	// static class FlatUtil {
	// float mDelta;
	// float[] mValues = new float[3];
	//
	// FlatUtil(float delta) {
	// mDelta = delta;
	// }
	//
	// float[] flat(float[] newValues) {
	// for (int i = 0; i < mValues.length; i++) {
	// if (Math.abs(mValues[i] - newValues[i]) > mDelta) {
	//
	// mValues[i] = (mValues[i] - newValues[i] > 0 ? -1 : 1)
	// * mDelta + mValues[i];
	// }
	// }
	// return mValues;
	// }
	// }
}
